package com.example.administrator.im.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.administrator.im.R;
import com.example.administrator.im.adapter.SystemNotifycationAdapter;
import com.example.administrator.im.db.ContactInvitedDBHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

/**
 * create:2018/10/2 16:19
 * author: 姚鸿杰
 **/
public class SystemNotifycationActivity extends MySlideFinishActivity {


    private SystemNotifycationAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_notifycation);

        List<ContactInvitedDBHelper> list = new ArrayList<>();
        //按时间降序排序
        List<ContactInvitedDBHelper> contactInviteds = LitePal.order("time desc").find(ContactInvitedDBHelper.class);
        for (int i = 0; i < contactInviteds.size(); i++) {
            list.add(contactInviteds.get(i));
        }

//        Collections.sort(list);

        adapter = new SystemNotifycationAdapter(list, this);
        RecyclerView rv = (RecyclerView) findViewById(R.id.asn_recycler);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);

        EventBus.getDefault().register(this);
    }

    public void iv_finish(View v) {
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateItem(UpdateSystemNotifycationItem item) {
        adapter.addItemInFirst(item.contactInvited);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public static class UpdateSystemNotifycationItem {
        private ContactInvitedDBHelper contactInvited;

        public UpdateSystemNotifycationItem(ContactInvitedDBHelper contactInvited) {
            this.contactInvited = contactInvited;
        }

        public ContactInvitedDBHelper getContactInvited() {
            return contactInvited;
        }
    }

}
